package com.applet.utils.common;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;

public class JSONUtil {

	public static void main(String[] args){

		String str="{\"服务时长\":\"3\",\"房屋面积\":\"100\"}";
		Map<String,Object> map = getMap(str);
		System.out.print("KEY："+map.get("服务时长").toString());
	}

	public static String toJSONString(Object object) {
		try {
			ObjectMapper objectMapper = new ObjectMapper();
			return objectMapper.writeValueAsString(object);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static <T> T parseObject(String text, Class<T> clazz) {
		try {
			ObjectMapper objectMapper = new ObjectMapper();
			return objectMapper.readValue(text, clazz);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * String字符串转MAP
	 * @return
	 */
	public static Map<String,Object> getMap(String str){
		Map<String,Object> map=new HashMap<String,Object>();
		try{
			Gson gson = new Gson();
			map=gson.fromJson(str,map.getClass());
		}catch (Exception e){
			e.getStackTrace();
		}
		return map;
	}
}
